home *** CD-ROM | disk | FTP | other *** search
/ The Arsenal Files 8 / The Arsenal Files Collection #8 (Arsenal Computer) (1996).ISO / prg_casm / snip9611.zip / STRFTIME.C < prev    next >
C/C++ Source or Header  |  1996-11-24  |  10KB  |  337 lines

  1. /* +++Date last modified: 02-Nov-1995 */
  2.  
  3. /**
  4.  *
  5.  * strftime.c
  6.  *
  7.  * implements the ansi c function strftime()
  8.  *
  9.  * written 6 september 1989 by jim nutt
  10.  * released into the public domain by jim nutt
  11.  *
  12.  * modified 21-Oct-89 by Rob Duff
  13.  *
  14. **/
  15.  
  16. #include <stddef.h>     /* for size_t */
  17. #include <stdarg.h>     /* for va_arg */
  18. #include <time.h>       /* for struct tm */
  19. #include "strftime.h"
  20.  
  21. static char *aday[] = {
  22.     "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
  23. };
  24.  
  25. static char *day[] = {
  26.     "Sunday", "Monday", "Tuesday", "Wednesday",
  27.     "Thursday", "Friday", "Saturday"
  28. };
  29.  
  30. static char *amonth[] = {
  31.     "Jan", "Feb", "Mar", "Apr", "May", "Jun",
  32.     "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
  33. };
  34.  
  35. static char *month[] = {
  36.     "January", "February", "March", "April", "May", "June",
  37.     "July", "August", "September", "October", "November", "December"
  38. };
  39.  
  40. static char buf[26];
  41.  
  42. static void strfmt(char *str, const char *fmt, ...);
  43.  
  44. /**
  45.  *
  46.  * size_t strftime(char *str,
  47.  *                 size_t maxs,
  48.  *                 const char *fmt,
  49.  *                 const struct tm *t)
  50.  *
  51.  *      this functions acts much like a sprintf for time/date output.
  52.  *      given a pointer to an output buffer, a format string and a
  53.  *      time, it copies the time to the output buffer formatted in
  54.  *      accordance with the format string.  the parameters are used
  55.  *      as follows:
  56.  *
  57.  *          str is a pointer to the output buffer, there should
  58.  *          be at least maxs characters available at the address
  59.  *          pointed to by str.
  60.  *
  61.  *          maxs is the maximum number of characters to be copied
  62.  *          into the output buffer, included the '\0' terminator
  63.  *
  64.  *          fmt is the format string.  a percent sign (%) is used
  65.  *          to indicate that the following character is a special
  66.  *          format character.  the following are valid format
  67.  *          characters:
  68.  *
  69.  *              %A      full weekday name (Monday)
  70.  *              %a      abbreviated weekday name (Mon)
  71.  *              %B      full month name (January)
  72.  *              %b      abbreviated month name (Jan)
  73.  *              %c      standard date and time representation
  74.  *              %d      day-of-month (01-31)
  75.  *              %H      hour (24 hour clock) (00-23)
  76.  *              %I      hour (12 hour clock) (01-12)
  77.  *              %j      day-of-year (001-366)
  78.  *              %M      minute (00-59)
  79.  *              %m      month (01-12)
  80.  *              %p      local equivalent of AM or PM
  81.  *              %S      second (00-59)
  82.  *              %U      week-of-year, first day sunday (00-53)
  83.  *              %W      week-of-year, first day monday (00-53)
  84.  *              %w      weekday (0-6, sunday is 0)
  85.  *              %X      standard time representation
  86.  *              %x      standard date representation
  87.  *              %Y      year with century
  88.  *              %y      year without century (00-99)
  89.  *              %Z      timezone name
  90.  *              %%      percent sign
  91.  *
  92.  *      the standard date string is equivalent to:
  93.  *
  94.  *          %a %b %d %Y
  95.  *
  96.  *      the standard time string is equivalent to:
  97.  *
  98.  *          %H:%M:%S
  99.  *
  100.  *      the standard date and time string is equivalent to:
  101.  *
  102.  *          %a %b %d %H:%M:%S %Y
  103.  *
  104.  *      strftime returns the number of characters placed in the
  105.  *      buffer, not including the terminating \0, or zero if more
  106.  *      than maxs characters were produced.
  107.  *
  108. **/
  109.  
  110. size_t strftime(char *s, size_t maxs, const char *f, const struct tm *t)
  111. {
  112.       int w;
  113.       char *p, *q, *r;
  114.  
  115.       p = s;
  116.       q = s + maxs - 1;
  117.       while ((*f != '\0'))
  118.       {
  119.             if (*f++ == '%')
  120.             {
  121.                   r = buf;
  122.                   switch (*f++)
  123.                   {
  124.                   case '%' :
  125.                         r = "%";
  126.                         break;
  127.  
  128.                   case 'a' :
  129.                         r = aday[t->tm_wday];
  130.                         break;
  131.  
  132.                   case 'A' :
  133.                         r = day[t->tm_wday];
  134.                         break;
  135.  
  136.                   case 'b' :
  137.                         r = amonth[t->tm_mon];
  138.                         break;
  139.  
  140.                   case 'B' :
  141.                         r = month[t->tm_mon];
  142.                         break;
  143.  
  144.                   case 'c' :
  145.                         strfmt(r, "%0 %0 %2 %2:%2:%2 %4",
  146.                               aday[t->tm_wday], amonth[t->tm_mon],
  147.                               t->tm_mday,t->tm_hour, t->tm_min,
  148.                               t->tm_sec, t->tm_year+1900);
  149.                         break;
  150.  
  151.                   case 'd' :
  152.                         strfmt(r,"%2",t->tm_mday);
  153.                         break;
  154.  
  155.                   case 'H' :
  156.                         strfmt(r,"%2",t->tm_hour);
  157.                         break;
  158.  
  159.                   case 'I' :
  160.                         strfmt(r,"%2",(t->tm_hour%12)?t->tm_hour%12:12);
  161.                         break;
  162.  
  163.                   case 'j' :
  164.                         strfmt(r,"%3",t->tm_yday+1);
  165.                         break;
  166.  
  167.                   case 'm' :
  168.                         strfmt(r,"%2",t->tm_mon+1);
  169.                         break;
  170.  
  171.                   case 'M' :
  172.                         strfmt(r,"%2",t->tm_min);
  173.                         break;
  174.  
  175.                   case 'p' :
  176.                         r = (t->tm_hour>11)?"PM":"AM";
  177.                         break;
  178.  
  179.                   case 'S' :
  180.                         strfmt(r,"%2",t->tm_sec);
  181.                         break;
  182.  
  183.                   case 'U' :
  184.                         w = t->tm_yday/7;
  185.                         if (t->tm_yday%7 > t->tm_wday)
  186.                               w++;
  187.                         strfmt(r, "%2", w);
  188.                         break;
  189.  
  190.                   case 'W' :
  191.                         w = t->tm_yday/7;
  192.                         if (t->tm_yday%7 > (t->tm_wday+6)%7)
  193.                               w++;
  194.                         strfmt(r, "%2", w);
  195.                         break;
  196.  
  197.                   case 'w' :
  198.                         strfmt(r,"%1",t->tm_wday);
  199.                         break;
  200.  
  201.                   case 'x' :
  202.                         strfmt(r, "%3s %3s %2 %4", aday[t->tm_wday],
  203.                               amonth[t->tm_mon], t->tm_mday, t->tm_year+1900);
  204.                         break;
  205.  
  206.                   case 'X' :
  207.                         strfmt(r, "%2:%2:%2", t->tm_hour,
  208.                               t->tm_min, t->tm_sec);
  209.                         break;
  210.  
  211.                   case 'y' :
  212.                         strfmt(r,"%2",t->tm_year%100);
  213.                         break;
  214.  
  215.                   case 'Y' :
  216.                         strfmt(r,"%4",t->tm_year+1900);
  217.                         break;
  218.  
  219.                   case 'Z' :
  220.                         r = (t->tm_isdst && tzname_[1][0]) ?
  221.                               tzname_[1] : tzname_[0];
  222.                         break;
  223.  
  224.                   default:
  225.                         buf[0] = '%';     /* reconstruct the format */
  226.                         buf[1] = f[-1];
  227.                         buf[2] = '\0';
  228.                         if (buf[1] == 0)
  229.                               f--;        /* back up if at end of string */
  230.                   }
  231.                   while (*r)
  232.                   {
  233.                         if (p == q)
  234.                         {
  235.                               *q = '\0';
  236.                               return 0;
  237.                         }
  238.                         *p++ = *r++;
  239.                   }
  240.             }
  241.             else
  242.             {
  243.                   if (p == q)
  244.                   {
  245.                         *q = '\0';
  246.                         return 0;
  247.                   }
  248.                   *p++ = f[-1];
  249.             }
  250.       }
  251.       *p = '\0';
  252.       return p - s;
  253. }
  254.  
  255. /*
  256.  *  stdarg.h
  257.  *
  258. typedef void *va_list;
  259. #define va_start(vp,v) (vp=((char*)&v)+sizeof(v))
  260. #define va_arg(vp,t) (*((t*)(vp))++)
  261. #define va_end(vp)
  262.  *
  263.  */
  264.  
  265. static int pow[5] = { 1, 10, 100, 1000, 10000 };
  266.  
  267. /**
  268.  * static void strfmt(char *str, char *fmt);
  269.  *
  270.  * simple sprintf for strftime
  271.  *
  272.  * each format descriptor is of the form %n
  273.  * where n goes from zero to four
  274.  *
  275.  * 0    -- string %s
  276.  * 1..4 -- int %?.?d
  277.  *
  278. **/
  279.  
  280. static void strfmt(char *str, const char *fmt, ...)
  281. {
  282.       int ival, ilen;
  283.       char *sval;
  284.       va_list vp;
  285.  
  286.       va_start(vp, fmt);
  287.       while (*fmt)
  288.       {
  289.             if (*fmt++ == '%')
  290.             {
  291.                   ilen = *fmt++ - '0';
  292.                   if (ilen == 0)                /* zero means string arg */
  293.                   {
  294.                         sval = va_arg(vp, char*);
  295.                         while (*sval)
  296.                               *str++ = *sval++;
  297.                   }
  298.                   else                          /* always leading zeros */
  299.                   {
  300.                         ival = va_arg(vp, int);
  301.                         while (ilen)
  302.                         {
  303.                               ival %= pow[ilen--];
  304.                               *str++ = (char)('0' + ival / pow[ilen]);
  305.                         }
  306.                   }
  307.             }
  308.             else  *str++ = fmt[-1];
  309.       }
  310.       *str = '\0';
  311.       va_end(vp);
  312. }
  313.  
  314. #ifdef TEST
  315.  
  316. #include <stdio.h>      /* for printf */
  317. #include <time.h>       /* for strftime */
  318.  
  319. char test[80];
  320. char * tzname_[2] = {"CST", "CDT"};
  321.  
  322. int main(int argc, char *argv[])
  323. {
  324.       int len;
  325.       char *fmt;
  326.       time_t now;
  327.  
  328.       time(&now);
  329.  
  330.       fmt = (argc == 1) ? "%I:%M %p\n%c\n" : argv[1];
  331.       len = strftime(test,sizeof test, fmt, localtime(&now));
  332.       printf("%d: %s\n", len, test);
  333.       return !len;
  334. }
  335.  
  336. #endif /* TEST */
  337.